#include <stm32f10x.h>
#include <string.h>
#include <stdio.h>
#include <ParseString.h>
#include <stdlib.h>

/**************************************************************************
 * Function Name: substring
 * Input: index start, index stop
 * Output : sub string
 * Description: get substring
 * Example:
 * substring(0,5,"vagabond",dst);
 * ==> dst = "vagab"
 **************************************************************************/
void substring(unsigned int start, unsigned int stop, const char *src,
		char *dst) {
	//	unsigned int pos;
	//	unsigned int pos_dst = 0;
	//	for (pos = start; pos < stop; pos++) {
	//		dst[pos_dst++] = src[pos];
	//	}
	//	dst[pos_dst] = '\0';
	unsigned int pos;
	//	unsigned int pos_dst = 0;
	for (pos = start; pos < stop; pos++) {
		*dst = src[pos];
		dst++;
	}
	*dst = '\0';
	//	if (start <= stop) {
	//		int count = stop - start;
	//		sprintf(dst, "%.*s", count, src + start);
	//	}
}

/**************************************************************************
 * simulate strtok without danger
 *
 * example: strin = "hantrieuvu";
 * StringToken(strin , 'v' , strout);
 * => strin = "hantrieu";
 * =>strout = "u";
 **************************************************************************/
void StringTokenc(char *str_in, char chr_break, char *str_out) {
	unsigned int pos = 0;
	unsigned int len = strlen(str_in);
	for (pos = 0; pos < len; pos++) {
		if (str_in[pos] == chr_break) {
			substring(pos + 1, len, str_in, str_out);
			substring(0, pos, str_in, str_in);
			break;
		}
	}
}

/**************************************************************************
 * simulate strtok without danger
 *
 * example: strin = "hantrieuvu";
 * StringToken(strin , "trieu" , strout);
 * => strin = "han";
 * =>strout = "vu";
 **************************************************************************/
void StringTokens(char *str_in, const char *str_break, char *str_out) {
	unsigned int pos_brk = 0;
	char str_tmp[2048];
	StringTokenc(str_in, str_break[pos_brk], str_out);
	for (pos_brk = 1; pos_brk < strlen(str_break); pos_brk++) {
		strcpy(str_tmp, str_out);
		StringTokenc(str_tmp, str_break[pos_brk], str_out);
	}
}

/**************************************************************************
 * find position of a string in another larger one
 *
 * return -1 if cannot find
 * 		  ><-1 if found
 **************************************************************************/
int FindInString(const char *str_in, const char *str_fnd) {
	unsigned int pos;
	unsigned int pos_mark = 0;
	unsigned int pos_fnd = 0;
	unsigned int len = strlen(str_in);
	unsigned int len_fnd = strlen(str_fnd);
	for (pos = 0; pos < len; pos++) {
		if (str_in[pos] == str_fnd[pos_fnd]) {
			pos_mark = pos;
			do {
				if (pos_fnd == len_fnd - 1) {
					return (int) pos_mark;
				}
				pos++;
				pos_fnd++;
				if (str_in[pos] != str_fnd[pos_fnd]) {
					pos_fnd = 0;
					break;
				}
			} while (pos_fnd < len_fnd);
		}
	}
	return -1;
}

/**************************************************************************
 * cut off an expected part from a string
 * str_in: input string
 * part: part to eleminate
 *
 * return 1: found and cut
 * 		  0: end of string, part doesn't exist
 **************************************************************************/
unsigned int StringCut(char *str_in, const char *part) {
	int res = FindInString(str_in, part);
	unsigned int len_part = strlen(part);
	unsigned int len = strlen(str_in);
	if (res != -1) {
		while ((res + len_part) <= len) {
			str_in[res] = str_in[res + len_part];
			res++;
		}
		return 1;
	}
	return 0;
}

/***************************************************************************
 * cut right of string
 * str_in: input string
 * lenght: lenght of expected output string
 * str_out: output string
 *
 ***************************************************************************/
void RightString(const char *str_in, unsigned int lenght, char *str_out) {
	unsigned int len = strlen(str_in);
	unsigned int pos;
	if (lenght >= len) {
		strcpy(str_out, str_in);
	} else {
		for (pos = len - lenght; pos < len; pos++) {
			*str_out = str_in[pos];
			str_out++;
		}
		*str_out = '\0';
	}
}

/***************************************************************************
 * cut left of string
 * str_in: input string
 * lenght: lenght of expected output string
 * str_out: output string
 *
 ***************************************************************************/
void LeftString(const char *str_in, unsigned int lenght, char *str_out) {
	unsigned int pos;
	if (lenght >= strlen(str_in)) {
		strcpy(str_out, str_in);
	} else {
		for (pos = 0; pos < lenght; pos++) {
			*str_out = str_in[pos];
			str_out++;
		}
		*str_out = '\0';
	}
}

/****************************************************************************
 * remove to a particular line
 * assume a line end with "\r\n"
 * start of a string is a readable character (a,b,c....)
 * count first line is ZERO
 *
 * example RemovetoLine(str,2) mean remove line 0 and 1
 ****************************************************************************/
void RemovetoLine(char *str_in, unsigned int line) {
	unsigned int len;
	unsigned int pos_line = 0;
	unsigned int pos = 0;
	while (pos_line < line) {
		len = strlen(str_in);
		pos = FindInString(str_in, "\r\n");
		substring(pos + 2, len, str_in, str_in);
		pos_line++;
	}
}

/*************************************************************
 * Check if a string is a number string
 *
 * return 1 if true
 * 		  0 if false
 *************************************************************/
unsigned int CheckNumberString(char *str) {
	unsigned int pos = 0;
	for (pos = 0; pos < strlen(str); pos++) {
		if ((str[pos] < '0') || (str[pos] > '9')) {
			return 0;
		}
	}
	return 1;
}

